package com.yy.permissions_general.module.system.role.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yy.permissions_general.common.config.exception.base.ServiceException;
import com.yy.permissions_general.module.system.role.entity.Role;
import com.yy.permissions_general.module.system.role.mapper.RoleMapper;
import com.yy.permissions_general.module.system.role.service.RoleService;
import com.yy.permissions_general.module.system.roleAuth.service.RoleAuthService;
import com.yy.permissions_general.module.system.user.entity.UserRole;
import com.yy.permissions_general.module.system.user.service.UserRoleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Iterator;
import java.util.List;

/**
 * <p>
 * 角色 服务实现类
 * </p>
 *
 * @author YY
 * @since 2021-09-30
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {
    @Resource
    private RoleAuthService roleAuthService;
    @Resource
    private UserRoleService userRoleService;
    /**
    * 分页查询列表
    * @param role
    * @return
    */
    @Override
    public  Page<Role>selectPageList(Role role, int limit, int page)  {
        Page pageContext = new Page(page,limit);
        Page<Role> pageList = this.baseMapper.selectPage(pageContext, role);
        return pageList;
    }

    /**
    * 新增数据
    * @param role
    */
    @Override
    public int addRole(Role role) {
        int resultInt = checkDict(role,false);
        if (resultInt == 1){
            throw new ServiceException(400,"角色编码或名称重复~");
        }
        return baseMapper.insert(role);
    }

    /**
    * 修改数据
    * @param role
    */
    @Override
    public int editRole(Role role) {
        int resultInt = checkDict(role,true);
        if (resultInt == 1){
            throw new ServiceException(400,"角色编码或名称重复~");
        }
        return baseMapper.updateById(role);
    }

    /**
     * 判断是否重复添加
     * @param role 角色
     * @param isUpdate 是否修改
     * @return
     */
    private int checkDict(Role role, boolean isUpdate) {
        int resultInt = 0;
        QueryWrapper<Role> queryWrapper = new QueryWrapper<Role>();
        if (isUpdate){
            queryWrapper.ne("id",role.getId());
        }
        queryWrapper.eq("role_code",role.getRoleCode());
        Role checkRole = this.getOne(queryWrapper);
        if (checkRole != null){
            resultInt = 1;
            return resultInt;
        }
        queryWrapper = new QueryWrapper<Role>();
        if (isUpdate){
            queryWrapper.ne("id",role.getId());
        }
        queryWrapper.eq("role_name",role.getRoleName());
        checkRole = this.getOne(queryWrapper);
        if (checkRole != null){
            resultInt = 1;
        }
        return resultInt;
    }

    @Override
    public int updateRole(Role role) {
        return baseMapper.updateById(role);
    }

    /**
    * 查看详情
    * @param id
    * @return
    */
    @Override
    public Role getDetailsById(Integer id) {
        return baseMapper.selectById(id);
    }

    /**
    * 删除数据
    * @param id
    * @return
    */
    @Override
    public int deleteById(Integer id) {
        /*判断角色是否绑定用户*/
        QueryWrapper<UserRole> userRoleQueryWrapper = new QueryWrapper<UserRole>();
        userRoleQueryWrapper.eq("role_id",id);
        List<UserRole> userRoles = userRoleService.list(userRoleQueryWrapper);
        if (CollectionUtil.isNotEmpty(userRoles)){
            throw new ServiceException(400,"操作有误：已绑定用户~");
        }
        roleAuthService.deleteByRoleId(Convert.toStr(id));
        return baseMapper.deleteById(id);
    }

    /**
    * 批量删除
    * @param ids
    * @return
    */
    @Override
    public int deleteBatch(List<Long> ids) {
        QueryWrapper<UserRole> userRoleQueryWrapper;
        Iterator iterator = ids.iterator();
        while (iterator.hasNext()){
            /*判断角色是否绑定用户*/
            userRoleQueryWrapper = new QueryWrapper<UserRole>();
            userRoleQueryWrapper.eq("role_id",iterator.next());
            List<UserRole> userRoles = userRoleService.list(userRoleQueryWrapper);
            if (CollectionUtil.isNotEmpty(userRoles)){
                throw new ServiceException(400,"操作有误：已绑定用户~");
            }
            roleAuthService.deleteByRoleId(Convert.toStr(iterator.next()));
        }
        return baseMapper.deleteBatchIds(ids);
    }

}

